package com.xmindguoguo.boot.listener;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;

import com.xmindguoguo.boot.config.properties.ConfigurationKit;
import com.xmindguoguo.boot.modular.system.service.IAutoGenerateMenuService;
import com.xmindguoguo.boot.modular.system.service.ITSystemMenuService;

import lombok.extern.slf4j.Slf4j;

/**
 * 依赖于spring框架，在spring启动时调用 接近于加载完所有spring相关的启动
 * 
 * @ClassName ApplicationStartup
 * @author <a href="892042158@qq.com" target="_blank">于国帅</a>
 * @date 2018年5月4日 下午11:10:31 两种初始化方法都没法直接使用@Autowired来注入sevice进行数据库操作，于是我只好另辟蹊径，从spring的beanfactory中取得serice进行DB访问
 */
@Slf4j
@Service
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    ITSystemMenuService systemMenuService;
    @Autowired
    IAutoGenerateMenuService autoGenerateMenuService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext().getParent() != null) {
            return;
        }
        log.error("ApplicationStartup 启动了。。。。。。。。。。。");
        ApplicationContext applicationContext = event.getApplicationContext();
        WebApplicationContext wac = (WebApplicationContext) applicationContext;
        ServletContext context = wac.getServletContext();
        boolean autoGenerateMenuFlag = ConfigurationKit.CONFIG.getBoolean("autoGenerateMenuFlag", false); // 自动生成主键id
        if (autoGenerateMenuFlag) { // 是否自动生成权限菜单到数据库中
            autoGenerateMenuService.generMenuList(wac);
        }
    }

}